/*************************************************************
 * File: ParameterServiceImpl.java
 * Copyright (c) 2012
 * Author: Huang Shaobin
 * Email: Shaobin.Software@gmail.com Shaobin_Software@163.com 
 * Date: Mar 8, 2013
 * Description: 
 ************************************************************/

package snow.biz.system.service.impl;

import java.util.Collections;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import snow.biz.basic.service.IBaseService;
import snow.biz.basic.service.impl.HibernateBaseServiceImpl;
import snow.biz.system.model.Parameter;
import snow.biz.system.service.IParameterService;
import snow.core.domain.Pagination;
import snow.core.exceptions.BusinessException;
import snow.core.persistence.AndedConditions;
import snow.core.persistence.BinaryOperator;
import snow.core.persistence.Condition;
import snow.core.persistence.Conditions;
import snow.core.persistence.Order;
import snow.core.toolkit.Validator;

/**
 * 系统配置参数相关的服务实现类
 * 
 * @author Shaobin.Software@gmail.com Shaobin_Software@163.com
 * @version 1.0
 */
public class ParameterServiceImpl extends HibernateBaseServiceImpl implements IParameterService {

	public Integer createParameterFrom(Parameter parameter) throws BusinessException {
		Validator.notNull(parameter, "200120", "failure.system.parameter.not.null");
		final Conditions conditions = new AndedConditions();
		conditions.addCondition(new Condition("name", BinaryOperator.EQ, parameter.getName()));
		conditions.addCondition(new Condition("type", BinaryOperator.EQ, parameter.getType()));
		final List<Parameter> parameters = this.searchParametersFrom(conditions, IBaseService.EMPTY_ORDERS, 0, 1);
		Validator.isEmpty(parameters, "200123", "failure.system.parameter.exist");
		return super.save(parameter, Integer.class);
	}

	public void deleteParameterFrom(Parameter parameter) throws BusinessException {
	}

	public String getValueFrom(String name, String type, String defaultValue) {
		Conditions conditions = new AndedConditions();
		conditions.addCondition(new Condition("name", BinaryOperator.EQ, StringUtils.trim(name)));
		conditions.addCondition(new Condition("type", BinaryOperator.EQ, StringUtils.trim(type)));
		List<Parameter> list = null;
		try {
			final List<Order> orders = Collections.emptyList();
			list = this.searchParametersFrom(conditions, orders, 0, 1);
		} catch (BusinessException e) {
			log.error(e.getMessage(), e);
		}
		final Parameter parameter = CollectionUtils.isEmpty(list) ? null : list.get(0);
		final String value = parameter == null ? null : parameter.getValue();
		return StringUtils.isBlank(value) ? defaultValue : value;
	}

	public void modifyParameterFrom(Parameter parameter) throws BusinessException {
		// TODO Auto-generated method stub

	}

	public void removeParameterFrom(Parameter parameter) throws BusinessException {
		// TODO Auto-generated method stub

	}

	public Parameter searchParameterFrom(Integer id) throws BusinessException {
		return super.get(Parameter.class, id);
	}

	public Pagination searchParametersForPagingFrom(Conditions conditions, List<Order> orders, int start, int limit) throws BusinessException {
		return super.findForPaging(Parameter.class, conditions, orders, start, limit);
	}

	public List<Parameter> searchParametersFrom(Conditions conditions, List<Order> orders, int start, int limit) throws BusinessException {
		return super.find(Parameter.class, conditions, orders, start, limit);
	}

}
